package com.han.algo_ds.algorithm._21bloom_filter.mybf;

import org.junit.Test;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

/**
* @author hanpeng
* @version 创建时间：2020年7月21日 上午10:15:31
* @ClassName 类名称
* @Description 类描述
*/
public class MyBloomFilterTest {
	@Test
	public void testHashSize() {
		//1000000 可以这么写 1000_0000
		MyBloomFilter<Integer> bloomFilter = new MyBloomFilter<Integer>(10000_0000, 0.01);
		boolean put = bloomFilter.put(2);
		boolean put1 = bloomFilter.put(3);
		System.out.println(bloomFilter.contains(2));
	}
	/**
	 * 测试误判率
	 */
	@Test
	public void testP() {
		//1000000 可以这么写 1000_0000
		BloomFilter1<Integer> bf = new BloomFilter1<Integer>(1_00_0000, 0.01);
		for (int i = 1; i <= 1_00_0000; i++) {
			bf.put(i);
		}
		int count = 0;
		for (int i = 1_00_0000; i <= 2_00_0000; i++) {
			if (bf.contains(i)) {
				count++;
			}
		}
		System.out.println(count);
	}
	@Test
	public void testMyP() {
		//1000000 可以这么写 1000_0000
		MyBloomFilter<Integer> bf = new MyBloomFilter<Integer>(1_00_0000, 0.01);
		for (int i = 1; i <= 1_00_0000; i++) {
			bf.put(i);
		}
		int count = 0;
		for (int i = 1_00_0000; i <= 2_00_0000; i++) {
			if (bf.contains(i)) {
				count++;
			}
		}
		System.out.println(count);
	}
	@Test
	public void testUrlP() {
		// 数组
		String[] urls = {};
		//1000000 可以这么写 1000_0000
		MyBloomFilter<String> bf = new MyBloomFilter<String>(10_0000_0000, 0.01);
		//不存在 放进去 
		
		for (String url : urls) {
			//有些网址没有爬过 但是由于误判 返回true 所以导致漏爬 但绝对不会导致重复爬
			if (bf.contains(url)) continue;
			// 爬这个url
			// ......
			
			// 放进BloomFilter中
			bf.put(url);
		}
		for (String url : urls) {
			if (bf.put(url) == false) continue;
			// 爬这个url
			// ......
		}
		System.out.println("");
	}
	@Test
	public void testGuavaBloomFilter() {
		int size = 1_00_0000;//预计要插入多少数据

		double fpp =  0.01;//期望的误判率

		BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), 1_00_0000, fpp);
		for (int i = 1; i <= 1_00_0000; i++) {
			bf.put(i);
		}
		int count = 0;
		for (int i = 1_00_0000; i <= 2_00_0000; i++) {
			if (bf.mightContain(i)) {
				count++;
			}
		}
		System.out.println(count);
	}
}
